package com.bigdata;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;
import org.mortbay.jetty.security.Credential;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CleanInterceptor implements Interceptor {

    Map<String, String> headerMap = new HashMap<>();
    List<Event> eventList = new ArrayList<>();

    @Override
    public void initialize() {

    }

    /*
    清洗用户信息 password md5加密，phone：最后4为xxxx替换
     */
    @Override
    public Event intercept(Event event) {
        String body = new String(event.getBody());
//        System.out.println("body:" + body);
//        JSONObject jsonObject = JSON.parseObject(body);
//        String password = jsonObject.getString("password");
//        String phone = jsonObject.getString("phone");
//        String newPwd = encryptToMD5(password);
//        String newPhone = phone.substring(0, phone.length() - 4) + "xxxx";
//
//        jsonObject.put("password", newPwd);
//        jsonObject.put("phone", newPhone);
//
//        // 从body里面取到数据后，清洗之后再设置到body中
//        System.out.println("JSONObject：" + JSON.toJSONString(jsonObject));
//
//        return event;
        try {
            if (body.contains("user")) {
//            if("first".equals(event.getHeaders().get("type"))) {
                JSONObject jsonObject = JSON.parseObject(body);
                String password = jsonObject.getString("password");
                String phone = jsonObject.getString("phone");
                String newPwd = encryptToMD5(password);
                String newPhone = phone.substring(0, phone.length() - 4) + "xxxx";

                jsonObject.put("password", newPwd);
                jsonObject.put("phone", newPhone);

                // 从body里面取到数据后，清洗之后再设置到body中
                event.setBody(JSON.toJSONString(jsonObject).getBytes());

                return event;
            } else {
                // 非用户 或者 商品的 日志数据
                // 过滤掉，不往channel传递， return null;
                return event;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {

//        String body = "{\"user\":\"mark\",\"password\":\"111111\",\"phone\":\"19812344321\"}";
        String body = "afdasdfas";
        JSONObject jsonObject = JSON.parseObject(body);
        String password = jsonObject.getString("password");
        String phone = jsonObject.getString("phone");
        String newPwd = encryptToMD5(password);
        String newPhone = phone.substring(0, phone.length() - 4) + "xxxx";

        jsonObject.put("password", newPwd);
        jsonObject.put("phone", newPhone);

        // 从body里面取到数据后，清洗之后再设置到body中
        System.out.println(JSON.toJSONString(jsonObject));
    }

    public static String encryptToMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] hashedBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));

            // Convert byte array to a hexadecimal string
            StringBuilder sb = new StringBuilder();
            for (byte hashedByte : hashedBytes) {
                sb.append(Integer.toString((hashedByte & 0xff) + 0x100, 16).substring(1));
            }

            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            // Handle the exception as needed
            return null;
        }
    }

    @Override
    public List<Event> intercept(List<Event> list) {
        // list 中的event 清空
        eventList.clear();

        for (Event event : list) {
            Event result = intercept(event);
            if (result != null) {
                eventList.add(result);
            }
        }
        return eventList;
    }

    @Override
    public void close() {

    }

    public static class Builder implements Interceptor.Builder {

        @Override
        public Interceptor build() {
            return new CleanInterceptor();
        }

        @Override
        public void configure(Context context) {

        }
    }

}
